<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../includes/tag.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Wcms内容管理系统</title>
    <script type="text/javascript" src="<%=basePath%>resources/lib/jquery/jquery.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.core.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/i18n/grid.locale-cn.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/jquery.jqGrid.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/public.js"></script>
	
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/styles/themes/buss-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/jquery-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/ui.jqgrid.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/public.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/icon.css">

	<script type="text/javascript">
		$(document).ready(function(){						
			setGridData();
			loadTree();
			resize();
			$(window).resize(resize);
			
			$("#search-btn").button({iconCls:'icon-search',handler:function(){
				var jsonParam = syscore.formToJson($("form[name='_searchForm']"));
				$("#tabledata").jqGrid("setGridParam",{postData:jsonParam}).trigger("reloadGrid");				
			}});
			
			$("#sort-list").button({iconCls:'icon-add',handler:function(){
				updateSortPage("list");
				
			}});
			
			$("#sort-edit").button({iconCls:'icon-add',handler:function(){
				updateSortPage("edit");
				
			}});
			
			$("#add-btn").button({iconCls:'icon-add',handler:function(){
				var node = $("#sysmenuTree").treeview("getSelectedNode");
				if(!node){
					$.alert('提示','请选择添加页面模型的功能菜单!',300,140,'info');
					return;
				}
				var url = '<%=basePath%>admin/datamodel/addpage.do';
				var addUrl = '<%=basePath%>admin/datamodel/add.do';
				var dlg = syscore.editDialog('新增页面模型信息',url,'id='+node.id,600,460,addUrl,function(rsData){				
					if(rsData.message.code != '0'){
						 $.alert('提示','保存数据失败!原因:' + rsData.message.mess,300,140,'error');
						 return false;
					 }else{
	       			 	 $("#tabledata").jqGrid().trigger('reloadGrid');				        			 	
	       			 	 dlg.dialog("destroy");
					 }	
				},function(){
					dlg.find("select[name='propertyType']").combo();
				});
				
				
			}});		
			$("#edit-btn").button({iconCls:'icon-edit',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows == 0){
					$.alert('提示','你没有选择要修改的数据!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}
				var url = '<%=basePath%>admin/datamodel/editpage.do';
				var editUrl = '<%=basePath%>admin/datamodel/edit.do';
				var dlg = syscore.editDialog('修改页面模型信息',url,'id='+selRows[0],600,460,editUrl,function(rsData){
					if(rsData.message.code != '0'){
						 $.alert('提示','保存数据失败!原因:' + rsData.message.mess,300,140,'error');
						 return false;
					 }else{						 			
        			 	 $("#tabledata").jqGrid().trigger('reloadGrid');			        			 	
	       			 	 dlg.dialog("destroy");
					 }	
				},function(){
					dlg.find("select[name='propertyType']").combo();
				});	
			}});
			$("#del-btn").button({iconCls:'icon-remove',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows == 0){
					$.alert('提示','你没有选择要删除的数据!',300,140,'info');
					return;
				}
				$.confirm('提示','确认要删除选中数据吗?',300,140,function(){
					var str = "";
					for(var i = 0;i < selRows.length;i++){
						if(i < selRows.length - 1){
							str += "ids=" + selRows[i] + "&";
						}else{
							str += "ids=" + selRows[i];
						}
					}
					$.post("<%=basePath%>admin/datamodel/delete.do",str,function(rsData){
						$("#tabledata").jqGrid({page:1}).trigger('reloadGrid');
					},"json");
				});				
			}});
			
		});
		
		function updateSortPage(type){
			var node = $("#sysmenuTree").treeview("getSelectedNode");
			if(!node){
				$.alert('提示','请选择要排序的页面模型所属的功能菜单!',300,140,'info');
				return;
			}
			var url = "";
			if(type == "list"){
				url = '<%=basePath%>admin/datamodel/listsortpage/'+node.id+".do";
			}else{
				url = '<%=basePath%>admin/datamodel/editsortpage/'+node.id+".do";
			}
			var dlg = $("<div/>").dialog({
				modal:true,
				header:true,
				height:510,
				width:400,
				title:type == 'list'?'列表页面模型排序':'编辑页面模型排序',
				iconCls:'icon',
				loadUrl:url,
				buttons:[
				   {text:'确  定',type:'button',iconCls:'icon-save',handler:function(){
					   if(dlg.find("select option").length == 0) return;
					   dlg.find("select").attr("multiple",'multiple');
					   dlg.find("select option").attr("selected","selected");
					   var params = dlg.find("form").serialize();
					   var sortUrl = "<%=basePath%>admin/datamodel/updateSort.do";
					   $.post(sortUrl,params,function(rsData){
						   if(rsData.message.code != "0"){
							   $.alert('提示','设置排序序号失败!原因:' + rsData.message.mess,300,140,'info');
							   return;
						   }
						   $.alert('提示','设置排序序号成功',300,140,'info',function(){
							   dlg.dialog("destroy");
						   });
						   
					   },"json");
				   }},
				   {text:'关  闭',type:'button',iconCls:'icon-cancel',handler:function(){
					   dlg.dialog("destroy");
				   }}
				],
				onLoad:function(){
					dlg.find("#up_btn").button({handler:function(){
						var $opt = dlg.find("select option:selected:first");
						if(!$opt) return;
						$opt.prev().before($opt);
					}});
					dlg.find("#down_btn").button({handler:function(){
						var $opt = dlg.find("select option:selected:first");
						if(!$opt) return;
						$opt.next().after($opt);
					}});
				}
			});
			dlg.dialog("loadPage");
		}
		
		
		function setGridData(){
			var url = "<%=basePath%>admin/datamodel/list/0.do";
			var colNames= ['id','属性名称','默认标题', '显示标题','显示类型','列表显示','编辑页显示','用于搜索'];
			var colModel= [
				{name:'id',index:'id', width:60, hidden:true},
			    {name:'propertyName',index:'propertyName', width:100,align:'left'},
				{name:'defTitle',index:'defTitle',width:100,align:'left'},
				{name:'title',index:'title',width:100,align:'left'},
				{name:'propertyType.value',index:'propertyType',width:80,align:'left'},
				{name:'listIsShow',index:'listIsShow',width:60,align:'center',formatter:function(cellValue){
					if(cellValue == true){
						return "显示";
					}else{
						return "不显示";
					}
				}},
				{name:'editIsShow',index:'editIsShow',width:60,align:'center',formatter:function(cellValue){
					if(cellValue == true){
						return "显示";
					}else{
						return "不显示"
					}
				}},
				{name:'isSearch',index:'isSearch',width:60,align:'center',formatter:function(cellValue){
					if(cellValue == true){
						return "用于搜索";
					}else{
						return "不用于搜索";
					}
				}}
			    
			];
			syscore.setGridData("#tabledata","",url,colNames,colModel,true);
		}	
		
		function loadTree(){
			var root = [{
				id:"0",
				code:'menu_root',
				name:'系统菜单信息',			
				isParent:true,
				hasNode:true,
				open:true
			}];
			$("#sysmenuTree").treeview({
				treeNodeKey:'id',
				treeNodeParentKey:'parentId',
				hasChildKey:'hasNode',
				nameCol:'name',
				nodesCol:'nodes',
				expandSpeed:"",
				asyncParam:['parentId'],
				async:true,
				root:{isRoot:true,nodes:root},
				asyncUrl:function(treeNode){
					if(treeNode){
						return "<%=basePath%>admin/sysmenu/listByTree/"+treeNode.id+".do";
					}else{
						return "<%=basePath%>admin/sysmenu/listByTree/0.do";
					}
				},
				callback:{
					beforeAsyncSuccess:function(msg){
						if(msg.message.code == '-2'){
							$.alert('提示','你还没有登录或Session已经过期，请重新登录系统!',300,170,'info');
							return false;
						}
						if(msg.message.code == "-1"){
							$.alert("提示","获取信息失败，原因：" + msg.message.mess,300,170,'error');
							return false;
						}
						return true;
					},
					click:function(event,treeId,treeNode){
						var url = $("#tabledata").jqGrid("getGridParam","url");						
						$("#tabledata").jqGrid("setGridParam",{postData:null,"url":"<%=basePath%>admin/datamodel/list/" + treeNode.id + ".do"}).trigger('reloadGrid');
						
					}
				}
			});
		}
		
		function resize(){
			var h = $(window).height() - $(".nav_curloc").outerHeight() - $(".nav_search").outerHeight() - $(".nav_btn_bar").outerHeight() - 10;
			$(".context").height(h);
			$("#tabledata").jqGrid("setGridHeight",h - 25);
			$("#tabledata").jqGrid("setGridWidth",$(window).width() - $("#sysmenuTree").parent().width() - 12);
		}
		
	</script>
  </head>
  
  <body>
  	<div style="border-right:1px solid #68C4E9;position:absolute;width:180px;height:100%;left:0;overflow:auto;">
  		<ul id="sysmenuTree" class="tree"></ul>
  	</div>
  	<div style="height:100%;margin:0 0 0 181px;position:relative;">
  		<div class="nav_curloc">
	    	<label>您的当前位置：<bstag:navbar menuId="${menuid}"/></label>
	    </div>
	    <div class="nav_search" style="height:28px;">
	   		<form action="" name="_searchForm">
		    	<table height="100%" cellpadding="0" cellspacing="0">
		    		<tr>
		    			<td valign="middle">属性名称：</td>
		    			<td valign="middle">
		    				<input type="text" size="25" maxlength="100" class="text" name="propertyName"/>
		    			</td>
		    			<td valign="middle" style="padding-left:10px;">默认标题：</td>
		    			<td valign="middle">
		    				<input type="text" size="25" maxlength="100" class="text" name="defTitle"/>
		    			</td>
		    			<td valign="middle" style="padding-left:10px;">显示标题：</td>
		    			<td valign="middle">
		    				<input type="text" size="25" maxlength="100" class="text" name="title"/>
		    			</td>
		    			<td style="padding-left:5px;">
		    				<a id="search-btn" href="javascript:void(0);">查 询</a>
		    			</td>
		    		</tr>
		    	</table>
	    	</form>
	    </div>
	    <div class="nav_btn_bar">
	    	<table height="100%" cellpadding="0" cellspacing="0">
	    		<tr>
	    			<td valign="middle">
	    				<a id="add-btn" href="javascript:void(0);">新 增</a>
	    			</td>
	    			<td valign="middle"" style="padding-left:5px;">
	    				<a id="edit-btn" href="javascript:void(0);">修 改</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="del-btn" href="javascript:void(0);">删 除</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="sort-list" href="javascript:void(0);">列表排序</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="sort-edit" href="javascript:void(0);">编辑页排序</a>
	    			</td>
	    		</tr>
	    	</table>
	    </div>
	    <div class="context">
	    	<table id="tabledata"></table>
	    	<div id="tablepage"></div>
	    </div>
  	</div> 
    
  </body>
</html>
